package com.practicesuite.unittests;

import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import junit.framework.TestCase;
import com.practicesuite.systemlayer.*;

public class RonaldJUnitTests extends TestCase  {

    
	//Test Cases will go here
    /** TODO: 
     * Login Functionality / Lockout after 3 Attempts
        CRUD Users(Doctor,Nurse,Patient)
        Lookup Patients by Name
        CRUD Appointments
        CRUD  Record
        CRUD Doctor Orders
        Create Invoice
     * @throws Exception 
     */
    
    
    public void testUpdateUserStatus() {
        Patient p = null;
        try {
            p = SystemDriver.getPatient("CS");
        } catch (Exception e) {
            e.printStackTrace();
        }
        assertNotNull(p);
        //SystemDriver.editUser(p, p.getUserID(), 3, "CS4".toCharArray(), false);
    }
    public void testInsertInvoiceIntoDatabase() {
        Patient patient = new Patient("", 3, 3, new Date(), "", "", "", 1, new MedicalInformation(1, "", "", ""), "", false);
        Date date = new Date();
        Map<Double, String> services = new HashMap<Double, String>();
        services.put(100.00, "Injection");
        services.put(20.00, "Office Fee");
        services.put(00.40, "Water Charge");
        services.put(35.20, "Child Noise");
        Map<Double, String> discounts = new HashMap<Double, String>();
        discounts.put(10.50, "First time visit coupon");
        Invoice result = new Invoice(patient, date, services, discounts, 99, "", 1);
        
       // assertTrue(SystemDriver.setInvoice(result));
    }
    
    public void testGetInvoiceFromDatabase() {
        try {
            List<Invoice> i = SystemDriver.getInvoice(45);
            assertNotNull(i);
            System.out.println(i.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void testGetAllUsers() {
        try {
            List<User> l = SystemDriver.getAllUsers();
            assertNotNull(l);
            assertTrue(l.size() > 3);
            System.out.println(l.size());
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void testCreateTreatmentRecord() {
//        int[] vitals = {3,3,4};
//        ArrayList<Prescription> p = new ArrayList();
//        
//        try {
//            TreatmentRecord t = new TreatmentRecord(1, new Date(),2
//                    , "Bad Hair",vitals, "He gone die", SystemDriver.getDoctorOrderFromID(1));
//           int s =  SystemDriver.createTreatmentRecord(t, 4);
//           assertEquals(1, s);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
    public void testGetUserAndSuspend() {
        Patient p = null;
        try {
            p = SystemDriver.getPatient("CS");
            SystemDriver.suspendAccount(p, p.getEUID(), p.getUserID());
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        assertNotNull(p);
        assertEquals(true, p.isSuspended());
    }
    
    public void testPrescriptionList() {
       // List<Prescription> p = null;
        
        try {
            List<Prescription> p = SystemDriver.getPrescriptionList(3);
            assertNotNull(p);
            Prescription x = p.get(0);
            assertEquals("Triamcinologone", x.getDrugsList());
//            '1', 'Triamcinologone', 'appy to skin', '3'

        }catch(Exception ex) {
            
        }
        
       
    }
    public void testCreateUser() {
//        Patient p = null;
//        try {
//            p = SystemDriver.createPatient("Test E", new Date(),
//                "Heaven", "4444444444", "Male", 33, "test@hah.com"
//                , "CS", "CS".toCharArray(), 3);
//        }
//        catch(Exception ex) {
//            
//        }
//        
//        assertNotNull(p);
    }
    
    public void getTotalPatientsSeenAndBilling() {
    
        int ps;
        double tb;
        try {
            ps = SystemDriver.getTotalPatientsSeen(1, 2011);
            assertEquals(20, ps);
            tb = SystemDriver.getTotalBilling(1, 2011);
            assertEquals(566.89, tb);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    public void testGetAllTreatmentRecords() {
        try {
            List<TreatmentRecord> tRecords = SystemDriver.getAllTreatmentRecords();
            System.out.println(tRecords.get(0));
            System.out.println(tRecords.size());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void testGetTreatmentRecord() {
        try {
        Patient p = SystemDriver.getPatient("Ron");
        assertNotNull(p);
        List<TreatmentRecord> to = SystemDriver.getTreatmentRecordList(p);
        assertNotNull(to);
        
        assertEquals(2, to.get(0).getAttendingNurse());
        }
        catch (Exception ex) {
            System.out.println(ex);
        }
        
    }
    public void testGetPatientList() {
        List<Patient> r = null;
        try {
            r = SystemDriver.getPatientList("Ron");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        
        assertNotNull(r);
    }
    public void testLogging() {
        try {
            SystemDriver.getPrescriptionList(9);
        } catch (Exception e) {
        }
    }
    
    public void testGetPatientByName() {
        Patient r = null;
        try {
            r = SystemDriver.lookupPatientByName("CS");
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        assertNotNull(r);
        assertEquals("Test E", r.getName());
    }
    
    
    public void testGetNurse() {
        try {
            Nurse n = SystemDriver.getNurseFromID(2);
            System.out.println(n);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void testGetDoctorList() {
        List<Doctor> d = null;
        try {
           d = SystemDriver.getDoctorList();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        for(Doctor dd : d) {
            System.out.println(dd.toString());
        }
        assertNotNull(d);
        
        
    }
    
    public void testGetAdmin() throws Exception {
        Admin a = SystemDriver.getAdmin("Dan", "Dan".toCharArray());
        
        System.out.println(a.getName());
        //System.out.println(a.geteUID());
    }
    
    public void testGetPatient() throws Exception {
        Patient p = SystemDriver.getPatient("Ron");
        System.out.println(p.getName());
        System.out.println(p.getDateOfBirth());
        assertNotNull(p);
    }
    
    public void testGetPassword() throws Exception {
        String s = SystemDriver.retrievePassword(1);
        assertEquals("James", s);
    }
    
    /**
     * This test will test the functionality of the Nurse class
     */
    public void testNurse()
        throws Exception {
        String name = "";
        int eUID = 1;
        int userID = 1;
        Calendar gcal = new GregorianCalendar(2011, Calendar.MARCH, 12);
        Date dateOfBirth = gcal.getTime();
        String address = "";
        String phoneNumber = "";
        String gender = "";
        int age = 1;
        //Create Nurse
        Nurse result = new Nurse(name, eUID, userID, dateOfBirth, address, phoneNumber, gender, age, "", false);
        assertNotNull(result);
        //Read Nurse
       // assertEquals("Nurse [toString()=User [name=, eUID=1, userID=1, dateOfBirth=03/12/2011, address=, phoneNumber=, gender=null, age=0]]", result.toString());
        assertEquals("", result.getAddress());
        assertEquals("", result.getName());
        assertEquals(1, result.getAge());
        assertEquals(1, result.getUserID());
        assertEquals(1, result.getEUID());
        assertEquals("", result.getPhoneNumber());
        assertEquals("", result.getGender());
        
        //Update Nurse
        result.setName("Steven McMcllin");
        result.setGender("Male");
        assertEquals("Steven McMcllin", result.getName());
        assertEquals("Male", result.getGender());
        
        //Test ability to create paitent and delete them
        MedicalInformation medicalInformation =
            new MedicalInformation(1, "", "", "");


        Patient p = result.createPatient("Joe Blow",3,867,gcal.getTime(),"300 Buzz Lane"
                , "555-555-5555", "male", 7, medicalInformation, "", false);
        assertEquals("Joe Blow", p.getName());
        p =  result.deletePatient(p); 
        assertNull(p);
        //Delete the Nurse
        result = null;
       
        assertNull(result);
    }

    /**
     * This method will test the functionality of the Doctor class
     */
    public void testDoctor() {
        
    }
    
    /**
     * This method will test Create,read,delete,update of a Patient
     */
    public void testPatient() {
        String name = "";
        int eUID = 1;
        int userID = 1;
        Date dateOfBirth = new Date();
        String address = "";
        String phoneNumber = "";
        String gender = "";
        int age = 1;
        MedicalInformation medicalInformation = new MedicalInformation(1, "", "", "");

        Patient result = new Patient(name, eUID, userID, dateOfBirth, address, phoneNumber, gender, age, medicalInformation, "", false);

        //Create Patient and Read data
        assertNotNull(result);
       // assertEquals("Patient [medicalInformation=MedicalInformation [userID=1, pharmCarrier=, allergies=[]], isLoggedIn=false, medicalHistory=null]", result.toString());
        assertEquals(false, result.systemLoggin());
        assertEquals(null, result.getMedicalHistory());
        assertEquals("", result.getAddress());
        assertEquals("", result.getName());
        assertEquals(1, result.getAge());
        assertEquals(1, result.getUserID());
        assertEquals(1, result.getEUID());
        assertEquals("", result.getPhoneNumber());
        assertEquals("", result.getGender());
        
        //Update Patient
        result.setAddress("400 Buzz Port Lane");
        result.setAge(99);
        assertEquals("400 Buzz Port Lane", result.getAddress());
        assertEquals(99, result.getAge());
        
        //Delete Patient
        result = null;
        assertNull(result);
        
    }

    /**
     * Test Login Functionality
     */
        @Test(expected=Exception.class)
    public void testLogin() {
            SystemDriver.getInstance();
            int i  = 0;
            try {
                assertEquals(3, SystemDriver.requestLogin("Ron"
                        , "gatech".toCharArray(), i));
                
            } catch (Exception e) {
            
        
                }
            
            i = 4;
            //Testing locked out, even if you type the correct password, 
            //you should still be shown the invalid login if attempts > 3
            try {
                SystemDriver.requestLogin("Ron", "testpw".toCharArray(), i);
            }
            catch (Exception e) {
                assertEquals("Your account has been locked, maximum trys reached"
                        , e.getMessage());
            }
            
        }

    
    public void testAppointment()
    throws Exception {
    int appointnmentType = 1;
    int appointnmentLength = 1;
    int createdBy = 1;
    boolean reSchedule = true;
    Patient patient = new Patient("", 1, 1, new Date(), "", "", "", 1, new MedicalInformation(1, "", "", ""), "", false);
    String reasonForApp = "Shotgun blew my head off";
    int desiredDoctorID = 1;

    Appointment result = new Appointment(appointnmentType, appointnmentLength, createdBy, reSchedule, patient, reasonForApp, desiredDoctorID, "day", "time");

    assertNotNull(result);
    //assertEquals("Appointment [appointnmentType=1, appointnmentLength=1, createdBy=1, reSchedule=true, patient=Patient [medicalInformation=MedicalInformation [userID=1, pharmCarrier=, allergies=[]], isLoggedIn=false, medicalHistory=null], reasonForApp=Shotgun blew my head off, desiredDoctorID=1]", result.toString());
    
    Appointment result2 = new Appointment();

    // add additional test code here
    assertNotNull(result2);
    assertEquals("Appointment [appointnmentType=0, appointnmentLength=30, createdBy=9, reSchedule=false, patient=null, reasonForApp=, desiredDoctorID=9]", result2.toString());

}
    
    public void testInvoice()
    throws Exception {
    Patient patient = new Patient("", 1, 1, new Date(), "", "", "", 1, new MedicalInformation(1, "", "", ""), "", false);
    Date date = new Date();
    Map<Double, String> services = new HashMap<Double, String>();
    services.put(100.00, "Injection");
    services.put(20.00, "Office Fee");
    services.put(00.40, "Water Charge");
    services.put(35.20, "Child Noise");
    Map<Double, String> discounts = new HashMap<Double, String>();
    discounts.put(10.50, "First time visit coupon");
    Invoice result = new Invoice(patient, date, services, discounts, 99, "", 1);

    // add additional test code here
    assertNotNull(result);
    System.out.println(result.toString());
    /*assertEquals("Under the C Invoice Statement \nName: \nPrinted: Tue Mar 15 19:49:21 EDT 2011\nInvoice Number: 0Date \t Description \t Cost\t \n----------------------------------------\n----------------------------------------\nDiscounts\n Reason: \t Amount: \n----------------------------------------\nTotal Savings: 0\nFinal Total: 0\n", result.toString());*/
    
}

}
